grammar calcAntlr;
/*
options{}  //
*/

/**/
@header {
    package lexico;
    import java.util.HashMap;
    import javax.swing.JOptionPane;
}
@lexer::header{
    package lexico;
}
@members {
    /** Map variable name to Integer object holding value */
    HashMap memory = new HashMap();
}

prog:   stat+ ;
           

stat:   expr NEWLINE { JOptionPane.showMessageDialog(null, "Resultado = " + $expr.value,
    "Resultado Operacion", JOptionPane.PLAIN_MESSAGE);}
;

 

expr returns [int value]:

        e=multExpr {$value = $e.value;}

        (   '+' e=multExpr {$value += $e.value;}

        |   '-' e=multExpr {$value -= $e.value;}

        )*

    ;

 

multExpr returns [int value]

    :   e=atom {$value = $e.value;} ('*' e=atom {$value *= $e.value;}

        |  '/' e=atom {

         if($e.value!=0)

              $value /= $e.value;

         else

              JOptionPane.showMessageDialog(null, "Divisi�n por cero!! ","Mensaje Error",JOptionPane.ERROR_MESSAGE);              // se dividen (o se lanza una excepci�n)

         }

        )*

    ;

 
atom returns [int value]
    :   INT {$value = Integer.parseInt($INT.text);}
    |  PARIZQ expr PARDER {$value = $expr.value;}    ;

PARIZQ : ('(')+;

PARDER : (')')+;  

INT :   '0'..'9'+ ;

NEWLINE:'\r'? '\n' ;

WS: (' '|'\t')+ {skip();} ;
//WS: (' '|'\t')+ {$setType(Token.SKIP);};

